package com.zcx.qshop.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.zcx.helper.activity.AppFragment;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.bound.BoundViewHelper;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.view.CheckView;
import com.zcx.helper.view.swipe.SwipeMenu;
import com.zcx.helper.view.swipe.SwipeMenuListView;
import com.zcx.qshop.QSApplication;
import com.zcx.qshop.R;
import com.zcx.qshop.action.LoginAction;
import com.zcx.qshop.action.ShoppingAction;
import com.zcx.qshop.activity.LoginActivity;
import com.zcx.qshop.adapter.ShopCarAdapter;
import com.zcx.qshop.conn.JsonShopcartAsyGet;
import com.zcx.qshop.conn.JsonShopcartdeleteAsyGet;
import com.zcx.qshop.conn.JsonShopcarteditAsyGet;
import com.zcx.qshop.conn.JsonUserAsyGet;
import com.zcx.qshop.menu.DeleteMenuCreator;
import com.zcx.qshop.view.CalculatorView;
import com.zcx.qshop.view.SelectView;
import com.zcx.qshop.view.TitleView;

import java.util.List;

/**
 * Created by Administrator on 2015/8/8 0008.
 */
public class ShopCarFragment extends AppFragment {

    @BoundView(R.id.title_view) private TitleView titleView;

    @BoundView(R.id.shop_car_list_view) private SwipeMenuListView swipeMenuListView;

    @BoundView(R.id.shop_car_select_view) private SelectView selectView;

    @BoundView(R.id.shop_car_total) private TextView total;

    @BoundView(R.id.shop_car_settle) private TextView settle;

    @BoundView(R.id.shop_car_login) private View login;

    @BoundView(R.id.shop_car_exit) private View exit;

    public ShopCarAdapter adapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = BoundViewHelper.boundView(this, QSApplication.ScaleScreenHelper.loadView((ViewGroup) inflater.inflate(R.layout.fragment_shop_car, null)));

        titleView.showStyle(TitleView.Style.FULL);

        titleView.showType(TitleView.Type.NAME_ONE);

        titleView.setTitleName("购物车");

        swipeMenuListView.setMenuCreator(new DeleteMenuCreator(getActivity()));

        swipeMenuListView.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {

            @Override
            public void onMenuItemClick(final int i, SwipeMenu swipeMenu, int i1) {

                new JsonShopcartdeleteAsyGet(QSApplication.QSPreferences.readUid(), adapter.getItem(i).cid, new AsyCallBack() {

                    @Override
                    public void onSuccess(String toast, int type, Object o) throws Exception {

                        QSApplication.ShoppingAction.removeData(i);

                        adapter.selectChange();

                    }

                }).execute(getActivity());

            }

        });

        QSApplication.ShoppingAction.setOnShopCarChangeCallBack(new ShoppingAction.OnShopCarChangeCallBack() {

            @Override
            public void onShopCarBack(List<JsonShopcartAsyGet.Info.Shop> shops) {

                swipeMenuListView.setAdapter(adapter = new ShopCarAdapter(getActivity(), shops) {

                    @Override
                    public void onCountChange(final JsonShopcartAsyGet.Info.Shop shop, final CalculatorView calculatorView, final int count) {

                        new JsonShopcarteditAsyGet(QSApplication.QSPreferences.readUid(), shop.cid, count + "", new AsyCallBack() {

                            @Override
                            public void onSuccess(String toast, int type, Object o) throws Exception {

                                calculatorView.setCount(count + "");

                                shop.number = count;

                                selectChange();

                            }

                        }).execute(getActivity());

                    }

                    @Override
                    protected void onTotalChange(String price) {

                        total.setText(price);

                    }

                    @Override
                    protected void onSelectAll(boolean isSelect) {

                        selectView.setCheck(isSelect, false);

                    }

                });

            }

            @Override
            public void onShopCarChange() {

                adapter.selectChange();

                selectView.setCheck(false);

            }

        });

        selectView.setOnCheckChangeListener(new CheckView.OnCheckChangeListener() {

            @Override
            public void onCheckChange(View var1, boolean var2) {

                adapter.setSelectAll(var2);

            }

        });

        settle.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                QSApplication.ShoppingAction.settle(getActivity());

            }

        });

        exit.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                startVerifyActivity(LoginActivity.class);

            }

        });

        QSApplication.LoginAction.addOnLoginStateListener(new LoginAction.OnLoginStateListener() {

            @Override
            public void onLogin(JsonUserAsyGet.Info info) {

                login.setVisibility(View.VISIBLE);

                exit.setVisibility(View.GONE);

                QSApplication.ShoppingAction.requestData(getActivity());

            }

            @Override
            public void onExit() {

                login.setVisibility(View.GONE);

                exit.setVisibility(View.VISIBLE);

            }

        });

        return view;

    }

}